package cn.simple;

import cn.simple.http.HttpRequest;
import cn.simple.run.SimpleConfiguration;
import cn.simple.run.SimpleConfigurationFactory;
import com.intellij.execution.ProgramRunnerUtil;
import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull;

public class SimpleAnAction extends AnAction {

    private final HttpRequest request;

    public SimpleAnAction(HttpRequest request) {
        this.request = request;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        if(event.getProject() == null) {
            return;
        }
        RunnerAndConfigurationSettings configuration = RunManager.getInstance(event.getProject()).createConfiguration("temp", SimpleConfigurationFactory.INSTANCE);
        configuration.setTemporary(true);
        ((SimpleConfiguration)configuration.getConfiguration()).setHttpRequest(request);
        ProgramRunnerUtil.executeConfiguration(configuration, DefaultRunExecutor.getRunExecutorInstance());
    }
}
